<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Login extends Controller {

    function Login() {
        parent::Controller();
    }
    
    
    function test_hash(){
        echo sha1("q8vkeubM");
    }

    function index() {
        //tampilin login form
        $this->form();
    }

    function forgetpassword() {
        $data['page'] = 'forgetpassword';
        $data['fp_form'] = $this->forget_pass_form();
        $data['valid'] = 0;
        $this->load->view('layout/main', $data);
    }

    function forget_pass_form() {
        $data['email'] = array(
            'id' => 'forgetPass',
            'name' => 'forgetPass'
        );
        $data['username'] = array(
            'id' => 'forgetPassUsr',
            'name' => 'forgetPassUsr'
        );
        return $data;
    }

    function generatePassword($username, $email) {
        //generate random password
            //ambil subsetnya, 10 karakter aja
            //hash SHA1, update database
        $newpass = substr(random_string(), 0, 10);
        $newpassSHA = sha1($newpass);

        $this->db->where("username", $username);
        $this->db->where("email", $email);
        
        $data = array(
            'password' => $newpassSHA
        );
        $this->db->update('users', $data);

        return $newpass;
    }

    function submitForgetPassword() {
        $email = $this->input->post('forgetPass');
        $username = $this->input->post('forgetPassUsr');

        $q = $this->db->query("SELECT email FROM users WHERE email='$email' AND username='$username'");

        if ($q->num_rows() > 0) {
            //valid
            $data['valid'] = 2;
            
            //kirim email

            $password = $this->generatePassword($username, $email);
            //config email
            $config['protocol'] = 'smtp';
            $config['smtp_host'] = 'localhost';
            $config['validate'] = 'TRUE';

           
            $this->load->library('email');
            $this->email->initialize($config);
            $this->email->set_newline("\r\n");
            $this->email->from('no-reply@iecom.itb.ac.id', 'IECOM 2012');
            $this->email->to($email);

            $this->email->subject('IECOM 2012 : Your Account\'s New Password');
            $this->email->message("Your account\'s password has been reset. Here is the details: ".
               "\n\nUsername : ". $username.
               "\nPassword : ". $password.
               "\n\nYou can now login with this new password");

            $this->email->send();
        } else {
            $data['valid'] = 1;
        }
        $data['page'] = 'forgetpassword';
        $data['fp_form'] = $this->forget_pass_form();

        $this->load->view('layout/main', $data);
    }

    function form() {
        $data['login_form']['form_name'] = array(
            'id' => 'loginform',
            'name' => 'loginform'
        );

        $data['login_form']['username'] = array(
            'id' => 'username_login',
            'name' => 'username_login'
        );

        $data['login_form']['password'] = array(
            'id' => 'password_login',
            'name' => 'password_login'
        );

        $data['login_form']['submit'] = array(
            'id' => 'submit_login',
            'name' => 'submit_login',
            'value' => 'Submit'
        );

        $data['page'] = 'login';

        $this->load->view('layout/main', $data);
    }

    function do_login() {
        $usr = $this->input->post('username_login');
        if ($this->validate_login()) {
            //create session
            $this->create_session($usr);
            //redirect ke halaman profile
            redirect(base_url() . index_page() . '/profile/home/', 'location');
        } else {
            //login failed
            $data['page'] = 'login_failed';
            $this->load->view('layout/main', $data);
        }
    }

    function validate_login() {
        $usernamelogin = $this->input->post('username_login');
        $passwordlogin = sha1($this->input->post('password_login'));

        $this->db->where('username', $usernamelogin);
        $this->db->where('password', $passwordlogin);
        $this->db->where('is_active', 1);
        $query = $this->db->get('users');
        if ($query->num_rows() == 1) {
            return true;
        } else {
            return false;
        }
    }

    function create_session($username) {
        $this->session->sess_destroy();
        //echo "berhasil";
        $this->session->sess_create();
        $userdata = array(
            'logged_in' => true,
            'username' => $username
        );
        $this->session->set_userdata($userdata);
        $data = array(
            'username' => $this->session->userdata('username'),
            'logged_in' => $this->session->userdata('logged_in'),
        );
        return $data;
    }

    function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . index_page(), 'location');
    }

}

?>
